
package com.lujianfei.productprice.ui.order;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lujianfei.productprice.R;
import com.lujianfei.productprice.base.BaseActivity;
import com.lujianfei.productprice.base.CommonAdapter;
import com.lujianfei.productprice.base.ViewHolder;
import com.lujianfei.productprice.utils.DateUtils;

import java.util.ArrayList;
import java.util.List;

public class OrderActivity extends BaseActivity implements OrderContract.View{

    OrderContract.Presenter presenter;
    List<OrderEntity> orderEntities = new ArrayList<>();
    BaseAdapter baseAdapter;
    ListView listview;
    LayoutInflater layoutInflater;
    ProgressBar progressBar;
    TextView txt_totalPrice;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        setTitleMiddleText("订单列表");
        layoutInflater = LayoutInflater.from(this);
        listview = findViewById(R.id.listview);
        progressBar = findViewById(R.id.progressBar);
        txt_totalPrice = findViewById(R.id.txt_totalPrice);
        presenter = new OrderPresenter(this);
        presenter.loadData();
        listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                OrderEntity itemAtPosition = (OrderEntity) parent.getItemAtPosition(position);
                presenter.showLongClickDialog(itemAtPosition);
                return true;
            }
        });
    }

    @Override
    protected int getTitleBarResId() {
        return R.id.custom_titlebar;
    }

    @Override
    public void setData(List<OrderEntity> orderEntities) {
        if (null == baseAdapter){
            baseAdapter = new CommonAdapter<OrderEntity>(this,this.orderEntities,R.layout.adapter_order_item) {

                @Override
                public void convert(ViewHolder viewHolder, OrderEntity data) {
                    viewHolder.setText(R.id.txtOrderId,"订单编号："+data.getId());
                    viewHolder.setText(R.id.txtOrderTotalPricde,String.format("¥ %.2f",data.getTotalPrice()));
                    viewHolder.setText(R.id.txtDate, DateUtils.getDateString(data.getCreateTime()));
                    LinearLayout layout_order_detail = viewHolder.getView(R.id.layout_order_detail);
                    layout_order_detail.removeAllViews();
                    List<OrderDetailEntity> orderDetailEntityList = data.getOrderDetailEntityList();
                    if (null == orderDetailEntityList || !orderDetailEntityList.isEmpty()){
                        for (OrderDetailEntity orderDetailEntity:
                        orderDetailEntityList) {
                            android.view.View detailView = layoutInflater.inflate(R.layout.adapter_order_detail_item,null,false);
                            TextView txtShopName = detailView.findViewById(R.id.txtShopName);
                            TextView txtProductName = detailView.findViewById(R.id.txtProductName);
                            TextView txtProductPrice = detailView.findViewById(R.id.txtProductPrice);
                            TextView txt_quantity = detailView.findViewById(R.id.txt_quantity);
                            txtShopName.setVisibility(orderDetailEntity.isShowShopName()?View.VISIBLE:View.GONE);
                            txtShopName.setText("店铺："+orderDetailEntity.getShopName());
                            txtProductName.setText(orderDetailEntity.getProductName());
                            txtProductPrice.setText(String.format("¥ %.2f",orderDetailEntity.getPrice()));
                            txt_quantity.setText(String.format("x%s",orderDetailEntity.getQuantity()));
                            layout_order_detail.addView(detailView);
                        }
                    }
                }
            };
            listview.setAdapter(baseAdapter);
        }
        this.orderEntities.clear();
        this.orderEntities.addAll(orderEntities);
        baseAdapter.notifyDataSetChanged();
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void showLoading() {
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideLoading() {
        progressBar.setVisibility(View.GONE);
    }

    @Override
    public void setTotalPrice(double totalPrice) {
        txt_totalPrice.setText(String.format("总收入：¥ %.2f",totalPrice));
    }
}
